package com.ss.controller;


import com.ss.bean.ZhCustomer;
import com.ss.mapper.ZhCustomerMapper;
import com.ss.result.R;
import com.ss.service.ZhCustomerService;
import com.ss.util.ExcelUtil;
import com.ss.vo.CellMessage;
import com.ss.vo.CustomerMessage;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 租赁合同 前端控制器
 * </p>
 *
 * @author lian
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/zhCustomer")
public class ZhRentContractController {

    @Resource
    private ZhCustomerService zhCustomerService;

    /**
     * 新增业主信息
     *
     * @param zhCustomer 前端传递的新业主信息
     * @return 业主信息是否新增成功
     */
    @PostMapping("/insertCustomer")
    public R insertCustomer(ZhCustomer zhCustomer) {
        System.out.println("insertCustomer");
        Integer result = zhCustomerService.insertCustomer(zhCustomer);
        if (result == 0) {
            return new R("业主编码已存在！");
        }
        return new R("添加业主信息成功！");
    }

    /**
     * 查询全部业主信息
     *
     * @return 全部业主信息
     */
    //@PostMapping("/selectCustomer")
    public R selectAllCustomer() {
        System.out.println("selectAllCustomer");
        List<ZhCustomer> zhCustomers = zhCustomerService.selectAllCustomer();
        return new R(zhCustomers);
    }

    /**
     * 根据用户选择的列和对应的值来查询业主信息
     *
     * @param column 对应查询的列
     * @param value  对应列的值
     * @return 业主信息
     */
    //@PostMapping("/selectCustomerByColumnAndValue")
    public R selectCustomerByColumnAndValue(@RequestParam("column") String column,
                                            @RequestParam("value") String value) {
        System.out.println("selectCustomerByColumnAndValue");
        List<ZhCustomer> zhCustomers = zhCustomerService.selectCustomerByColumnAndValue(column, value);
        return new R(zhCustomers);
    }

    /**
     * 根据业主类型来查询业主信息
     *
     * @param customerType 业主类型
     * @return 查询的业主信息
     */
    //@PostMapping("/selectByCustomerByCustomerType")
    public R selectByCustomerByCustomerType(String customerType) {
        System.out.println("selectByCustomerByCustomerType");
        List<ZhCustomer> zhCustomers = zhCustomerService.selectByCustomerByCustomerType(customerType);
        return new R(zhCustomers);
    }

    /**
     * 统一查询入口（优化查询功能）
     * 通过用户传递的参数进行对应的查询
     *
     * @return 查询结果：业主信息
     */
    @PostMapping("/selectCustomer")
    public R selectCustomer(@RequestBody CustomerMessage customerMessage) {
        System.out.println("selectCustomer");
        List<ZhCustomer> zhCustomers = zhCustomerService.selectCustomer(customerMessage);
        return new R(zhCustomers);
    }

    /**
     * 根据前端传递的业主编码信息来修改业主状态
     *
     * @param map 前端传递的参数
     *            customerCodes：此参数为业主编码，如果出现多个业主编码，会通过"|"进行分割
     *            status：状态：1启用 0禁用
     * @return 修改业主信息的状态结果
     */
    @PostMapping("/UpdateCustomerStatusByCustomerCode")
    public R UpdateCustomerStatusByCustomerCode(@RequestBody Map map) {
        System.out.println("UpdateCustomerStatusByCustomerCode");
        String customerCodes = (String) map.get("customerCodes");
        String status = (String) map.get("status");
        System.out.println("CustomerCodes：" + customerCodes + "status：" + status);
        Integer result = zhCustomerService.UpdateCustomerStatusByCustomerCode(customerCodes, status);
        if (result == 1) {
            return new R("修改成功！");
        } else {
            return new R("修改失败！");
        }
    }

    /**
     * 根据前端传递的Excel表格文件和对应的公司信息，解析出业主信息，存到数据库
     * @param file Excel表格文件（固定格式）
     * @param company 对应公司编号
     * @return 是否新增成功
     */
    @PostMapping("/uploadExcel")
    public R uploadExcel(MultipartFile file, String company) {
        System.out.println("uploadExcel");
        // 调用readExcel方法解析Excel表格
        if (file != null && file.getSize() > 0) {
            try {
                List<ZhCustomer> customers = ExcelUtil.readExcel(
                        (FileInputStream) file.getInputStream(),
                        ZhCustomer.class);
                for (ZhCustomer customer : customers) {
                    System.out.println(customer);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return new R();
    }
}

